﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomsSystem.Model;
using System.Data.SqlClient;
using System.IO;
using CustomsSystem.BLL;
using Maticsoft.Common;

namespace WEB.SQL
{
    public partial class Backup : System.Web.UI.Page
    {
        DBBackupManage BLL = new DBBackupManage();
        #region 页面加载
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)//判断页面是否首次加载
            {
                string XNpath = Server.MapPath(@"/save/" );
                this.Path.Text = XNpath+ "customer.bak";
                //查看文件夹的文件数
               string [] Files=  System.IO.Directory.GetFiles(XNpath);
               if(Files.Length>20)
               {
                    MessageBox.Show(this,"服务器数据已存在文件:"+Files.Length+"\n请及时备份、清理 ！");
               }

            }
        }
        #endregion

        #region 备份按钮
        protected void Backup_Click(object sender, EventArgs e)
        {

            //命名备份文件
            string BackUpName = DateTime.Now.ToString("yyyyMMddhhmmss") + "customs.bak";
            //备份地址
            string BKDBpath = Server.MapPath(@"/save/DB_" + BackUpName);
            string BKLOGpath = Server.MapPath(@"/save/LOG_" + BackUpName);
            //查看是否存在
            if(System.IO.File.Exists(BKDBpath)||System.IO.File.Exists(BKLOGpath))
            {
                BKDBpath = Server.MapPath(@"/save/DB_a_" + BackUpName);
                BKLOGpath = Server.MapPath(@"/save/LOG_a_" + BackUpName);
            }

            if (BLL.BackUp(BKDBpath, BKLOGpath))
            {

                MessageBox.Show(this, "备份成功");
                return;

            }
            MessageBox.Show(this, "备份失败，请联系管理员");

        }
        #endregion

        #region 还原按钮
        protected void Restore_Click(object sender, EventArgs e)
        {
            if (this.RestorePath.PostedFile.FileName != "")
            {
                //文件存入服务器，并获得地址
                string filename = this.RestorePath.FileName.ToString();
                string path = Server.MapPath(@"/App_Data/" + filename);
                this.RestorePath.SaveAs(path);

                if (BLL.ResDB(path))
                {
                    File.Delete(path);//删除文件
                    MessageBox.Show(this, "恢复成功");
                    return;
                }
                File.Delete(path);
                MessageBox.Show(this, "恢复失败");
            }


        }
        #endregion
    }
}